import request from '@/utils/request'
import localData from 'kuan-utils/lib/localData'

const { get, set } = localData

const aliKey = 'wfc-UPLOAD_TOKEN_WEDDING'
const qnKey = 'wfc-UPLOAD_TOKEN_QN'

/**
 * @param {Boolean} isPrivate
 */
export async function qnToken(isPrivate) {
  const type = isPrivate ? 'private' : 'public'
  const key = `${qnKey}-${module}`
  const localToken = get(key)
  if (localToken) return localToken
  const { token } = await request({
    url: '/api/dashboard/v1/uptoken',
    params: {
      type
    }
  })
  set(key, token, 60 * 24)
  return token
}

/**
 * 阿里云上传token
 */
export async function aliToken() {
  const localToken = get(aliKey)
  if (localToken) return localToken

  const { token } = await request({
    url: '/api/dashboard/v1/uptoken'
  })

  set(aliKey, token, 60 * 24)
  return token
}
